package org.ellisande.grid.tags;

import javax.servlet.jsp.JspWriter;

import org.ellisande.grid.tags.support.TagBuilder;
import org.ellisande.tld.annotations.Info;

@Info("Creates a button that expands into a dropdown menu. This tag expects list items (li) in the body.")
public class DropdownButtonTag extends ButtonTag {

	private static final long serialVersionUID = 1445985940516111731L;
	private static final String DROPDOWN_CLASS = "dropdown-toggle";
	


	@Override
	public void startTag() throws Exception {
		TagBuilder div = createTagBuilder("div");
		TagBuilder button = createTagBuilder("button");
		TagBuilder icon = createTagBuilder("i");
		TagBuilder list = createTagBuilder("ul");
		
		div.addStyle("btn-group");
		
		button.addStyle(BASE_STYLE);
		button.addStyle(type.toString());
		button.addStyle(size.toString());
		if(!disabled.isEmpty()){
			button.addStyle(disabled);
		}
		button.addStyle(DROPDOWN_CLASS);
		button.addAttribute("data-toggle", "dropdown");
		
		icon.addStyle("caret");
		
		list.addStyle("dropdown-menu");
		
		JspWriter out = pageContext.getOut();
		out.println(div.compileStartTag());
		out.println(button.compileStartTag());
		out.println(title);
		out.println(icon.compileStartTag()+icon.compileEndTag());
		out.println(button.compileEndTag());
		out.println(list.compileStartTag());
	}

	@Override
	public void endTag() throws Exception {
		TagBuilder div = createTagBuilder("div");
		TagBuilder list = createTagBuilder("ul");
		JspWriter out = pageContext.getOut();
		out.println(list.compileEndTag());
		out.println(div.compileEndTag());
	}

	@Override
	public void release() {
		super.release();
	}
}
